package com.airport.schedule.util;

import java.util.HashSet;
import java.util.Set;

public class SetUtil {

    public static <T> Set<T> intersect(Set<T> s1, Set<T> s2) {
        Set<T> common = new HashSet<>(Math.min(s1.size(),s2.size()));
        Set<T> toIter = s1.size() >= s2.size() ? s2 : s1;
        Set<T> toStay = toIter == s1 ? s2 : s1;
        for(T e : toIter) {
            if(toStay.contains(e))
                common.add(e);
        }
        return common;
    }

    public static <T> Set<T> subtract(Set<T> s1, Set<T> s2) {
        Set<T> result = new HashSet<>(s1.size());
        for(T e : s1) {
            if(!s2.contains(e))
                result.add(e);
        }
        return result;
    }

    public static <T> boolean hasIntersect(Set<T> s1, Set<T> s2) {
        Set<T> toIter = s1.size() > s2.size() ? s2 : s1;
        Set<T> toStay = toIter == s1 ? s2 : s1;
        for(T e : toIter) {
            if(toStay.contains(e)) {
                return true;
            }
        }
        return false;
    }

}
